<?php

namespace app\api\controller\product;

use app\BaseController;

use app\common\model\User\User as UserModel;
use app\common\model\Product\Product as ProductModel;
use app\common\model\Product\Type as TypeModel;
use app\common\model\Product\Vip as VipModel;
use app\common\model\Cart as CartModel;
use app\common\model\Order\Product as OrderProductModel;
use think\facade\Db;


/**
 * 用户表
 */
class Product extends BaseController
{

    public function initialize()
    {
        parent::initialize();

        $this->UserModel = new UserModel();
        $this->TypeModel = new TypeModel();
        $this->ProductModel = new ProductModel();
        $this->VipModel = new VipModel();
        $this->CartModel = new CartModel();
        $this->OrderProductModel = new OrderProductModel();

    }

    // 首页
    public function index(){
        if($this->request->isPost()){
            
            //查询推荐商品
            $Recommend = $this->ProductModel->with(['type'])->where(['flag' => 3])->order('id','desc')->limit(5)->select();

            //查询新品商品
            $News =  $this->ProductModel->with(['type'])->where(['flag' => 1])->order('id','desc')->limit(4)->select();

            //查询热销商品
            $Hot = $this->ProductModel->with(['type'])->where(['flag' => 2])->order('id','desc')->limit(6)->select();

            //查询分类
            $Type = $this->TypeModel->select();

            //封装数组
            $data = [
                'Recommend' => $Recommend,  //推荐商品
                'News' => $News,  //推荐商品
                'Hot' => $Hot,  //推荐商品
                'Type' => $Type,  //商品分类
            ];

            $this->success('返回首页数据', null, $data);
            exit;
        }
    }

    // 产品页面
    public function type() {
        if($this->request->isPost()){
          
            $typeid = $this->request->param('typeid', 0);
            $flag = $this->request->param('flag', 0);
            $orderby = $this->request->param('orderby', 'id');
            $keywords = $this->request->param('keywords', '');

            $where = [];

            //产品分类
            $type = $this->TypeModel->find($typeid);
            
            //有分类查询分类的产品，没有分类查询全部
            if($type)
            {
                $where['typeid'] = $typeid;
            }

            //判断是否有选择属性
            if($flag)
            {
                $where['flag'] = $flag;
            }

            if(!empty($keywords))
            {
            
                $where[] =  ['name', 'like', $keywords];
            }

            //获取商品列表
            $prolist = $this->ProductModel->where($where)->order($orderby,'DESC')->select();
            
             //查询分类
            $typelist = $this->TypeModel->select();

             $data = [
                 'prolist' => $prolist,
                 'typelist' => $typelist
             ];
 
            if($prolist === FALSE)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }else
            {
                $this->success('查询商品数据成功', null, $data);
                exit;
            }
        }
    }

    // 产品详情
    public function detail(){
        if($this->request->isPost()){
          
            $proid = $this->request->param('proid', 0);

            $userid = $this->request->param('userid', 0);

            //查询商品的详细
            $product = $this->ProductModel->with(['type'])->find($proid);

            if(!$product)
            {
                $this->error('商品不存在');
                exit;
            }

            if($userid){

                $user = $this->UserModel->find($userid);
                
                $vip = isset($user) ? $user['vip'] : 0;


                if($vip > 0)
                {
                    $level = $this->VipModel->where(['proid' => $proid, 'level' => $vip])->value('price');
                    $product['level'] = $level;
                }
                
                // 获取购物车数量
                $cartcount = $this->CartModel->where(['userid' => $userid])->count();

                if($cartcount){
                    $product['cartcount'] = $cartcount;
                }
            }

            // 平均分
            $average =  $this->OrderProductModel->where(['proid' => (string)$proid])->avg('rate');
           
            $product['average'] = $average;

            // $where = [ 'proid'=> (string)$proid, 'comment'=> "not null"];

            // 评价详情
            $ratelist = $this->OrderProductModel->withJoin('user') ->where('proid',(string)$proid)
            ->where('comment','not null')->select();

            // echo  $this->OrderProductModel->getLastSql();exit;

            $data = [
                'product' => $product,
                'ratelist' => $ratelist
            ];

            $this->success('返回商品详情', null, $data);
            exit;
        }
    }
}